<template>
  <div class="log-view-container">
    <div class="filter-wrapper">
      <div class="left">
        <el-input
          v-model="logKeyword"
          size="small"
          suffix-icon="el-icon-search"
          placeholder="输入关键字搜索"
          style="width: 240px; margin-right: 10px"
          clearable
        />
      </div>
      <div class="right">
        <el-select v-model="logPageTotal">
          <el-option
            v-for="item in logPageTotalList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
    </div>
    <div class="content">
      <div
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        class="log-view-wrapper"
      >
        <virtual-list
          ref="log-view"
          class="log-view"
          :data-key="'id'"
          :data-sources="items"
          :data-component="itemComponent"
          :keeps="logPageSize"
          :start="currentLogIndex - 1"
        />
      </div>
    </div>
  </div>
</template>

<script>
import LogItem from './LogItem'
import VirtualList from 'vue-virtual-scroll-list'
import Convert from 'ansi-to-html'
import hasAnsi from 'has-ansi'

const convert = new Convert()
export default {
  name: 'ImageBuildStageComponent',
  components: {
    VirtualList
  },
  props: {
    logContent: {
      type: String,
      default: () => { return '' }
    },
    loading: {
      type: Boolean,
      default: () => { return false }
    }
  },
  data() {
    return {
      itemComponent: LogItem,
      queryLoading: false,
      logPageSize: 50,
      logKeyword: '',

      logPageTotal: 1000,
      logPageTotalList: [{
        label: '500条',
        value: 500
      }, {
        label: '1000条',
        value: 1000
      }, {
        label: '2000条',
        value: 2000
      }]
    }
  },
  computed: {
    items() {
      const logItems = this.logContent.split('\n').slice(-this.logPageTotal)

      // 过滤
      const logFilterItems = logItems.filter(text => {
        if (!this.logKeyword) {
          return true
        }
        return !!text.toLowerCase().match(this.logKeyword.toLowerCase())
      })

      let index = 0
      return logFilterItems.map(itemText => {
        const isAnsi = hasAnsi(itemText)
        return {
          id: index++,
          text: isAnsi ? convert.toHtml(itemText) : itemText,
          searchString: this.logKeyword,
          isAnsi
        }
      })
    },
    currentLogIndex() {
      const page = this.items.length / this.logPageSize
      return page
    }
  },
  watch: {
  },
  mounted() {
  },
  methods: {
  }
}

</script>

<style lang="scss" scoped>
  .filter-wrapper {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }

  .content {
    display: block;
  }

  .log-view-wrapper {
    float: left;
    flex-basis: calc(100% - 40px);
    width: calc(100% - 5px);
    transition: width 0.3s;
  }

  .log-view-wrapper.errors-collapsed {
    flex-basis: 100%;
    width: 100%;
  }

  .log-view {
    margin-top: 0 !important;
    overflow-y: scroll !important;
    height: calc(100vh - 200px);
    list-style: none;
    color: #A9B7C6;
    background: #2B2B2B;
    border: none;
  }

  .errors-wrapper {
    float: left;
    display: inline-block;
    margin: 0;
    padding: 0;
    flex-basis: 240px;
    width: 300px;
    transition: opacity 0.3s;
    border-top: 1px solid #DCDFE6;
    border-right: 1px solid #DCDFE6;
    border-bottom: 1px solid #DCDFE6;
    height: calc(100vh - 240px);
    font-size: 16px;
    overflow: auto;
  }

  .errors-wrapper.collapsed {
    width: 0;
  }

  .errors-wrapper .error-list {
    list-style: none;
    padding: 0;
    margin: 0;
  }

  .errors-wrapper .error-list .error-item {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    /*height: 18px;*/
    border-bottom: 1px solid white;
    padding: 5px 0;
    background: #F56C6C;
    color: white;
    cursor: pointer;
  }

  .errors-wrapper .error-list .error-item.active {
    background: #E6A23C;
    font-weight: bolder;
    text-decoration: underline;
  }

  .errors-wrapper .error-list .error-item:hover {
    font-weight: bolder;
    text-decoration: underline;
  }

  .errors-wrapper .error-list .error-item .line-no {
    display: inline-block;
    text-align: right;
    width: 70px;
  }

  .errors-wrapper .error-list .error-item .line-content {
    display: inline;
    width: calc(100% - 70px);
    padding-left: 10px;
  }

  .right {
    display: flex;
    align-items: center;
  }

  .right .el-pagination {
    margin-right: 10px;
  }
</style>
